//引入样式
require('../less/drill.less')

//引入字体样式
require('../fonts/iconfont.css')


//引入样式
require('weui')
let weui = require('weui.js')



//文档加载事件
document.ready(function () {


    //判断用户是否登录
    until.isLogin()



    //获取元素
    const video = document.querySelector('#video')

    const up = document.querySelector('#up')
    const suspend = document.querySelector('#suspend')
    const next = document.querySelector('#next')


    const allMatter = document.querySelector('#allMatter')
    let curTitle = document.querySelector('#curTitle')

    const maskLayer = document.querySelector('#maskLayer')
    const courseImg = document.querySelector('#courseImg')
    const courseTitle = document.querySelector('#courseTitle')

    const go = document.querySelector('#go')
    const end = document.querySelector('#end')

    const progressBar = document.querySelector('#progressBar')




    //获取本地视频片段
    const frag = JSON.parse(localStorage.getItem('fragments'))


    //渲染总小结数
    allMatter.textContent = frag.length


    //定义变量接收视频片段索引
    let index = 0

    //定义初始时间
    let initTime = new Date()

    //定义时间数组
    let timearr = [];






    //播放
    function play() {
        setTimeout(function () {
            video.src = 'http://139.9.177.51:8099' + frag[index].videoUrl

            //渲染小结
            allMatter.textContent = index + 1
            //渲染当前小结标题
            curTitle.textContent = frag[index].title
            setwidth()
        }, 500)
    }
    play()

    //自动下一曲
    video.addEventListener('ended', function () {
        //索引自加
        ++index

        if (index >= frag.length) {
            console.log(index);
            return
        }


        play()
    })

    //点击下一曲
    next.addEventListener('click', function () {
        //索引自加
        ++index

        if (index >= frag.length) {
            index = frag.length - 1
            return
        }
        play()
    })

    //点击上一曲
    up.addEventListener('click', function () {
        //索引自减
        --index

        if (index < 0) {
            index = 0
            return
        }
        play()
    })

    //点击暂停
    suspend.addEventListener('click', function () {
        //显示遮罩层
        maskLayer.style.display = 'block'
        video.pause()//暂停
        courseImg.src = 'http://139.9.177.51:8099' + frag[index].imgUrl
        courseTitle.textContent = frag[index].title


        //暂停时间,将每次运动的时间添加至时间数组中
        timearr.push(new Date() - initTime)
    })

    //点击继续
    go.addEventListener('click', function () {
        //隐藏遮罩层
        maskLayer.style.display = 'none'

        //继续播放
        video.play()

        //下次运动的时间
        initTime = new Date()
    })





    //进度条


    function setwidth() {
        //公式    width/总width=当前视频播放时间/视频总时间
        setInterval(function () {
            progressBar.style.width = video.currentTime / video.duration * document.body.clientWidth + 'px'
        }, 30)
    }



    //结束
    end.addEventListener('click', function () {


        //定义总时间
        let allTime = 0;

        //处理总运动时间
        timearr.forEach(function (item) {
            allTime += item
        })
        

        let drilltime=(allTime / 1000 / 60).toFixed(2);


        //请求参数
        const params = {
            id: localStorage.getItem('course'),//课程id
            userId: localStorage.getItem('userId'),//用户id
            takeTime: drilltime,//训练时间
            calorie: 1.5*drilltime//消耗卡路里
        }


        //发送请求
        $http.post('/sports/saveTrain', params, function (res) {
            
            if( res.status===0 ){

                weui.toast('保存成功',{
                    duration:3000,
                    //className:'custom-classname',
                    callback:function(){
                        location.href = "course.html"
                    }
                })
                
            }

        })


        

    })
})